//
//  EmoticonViewController.swift
//  表情键盘
//
//  Created by 林柱强 on 16/2/27.
//  Copyright © 2016年 林柱强. All rights reserved.
//



import UIKit

private let EmoticonCellReuseIdentifier = "EmoticonCellReuseIdentifier"

class EmoticonViewController: UIViewController {
    /// 定义一个闭包属性，用于传递选中的表情模型
    var emoticonDidSelectedCallback: ((emoticon: Emoticon) -> ())
    
    init(callback: (emoticon: Emoticon) -> ()) {
        self.emoticonDidSelectedCallback = callback
        super.init(nibName: nil, bundle: nil)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = UIColor.blueColor()
        //初始化UI
        setupUI()
    }
    
    /**
     初始化UI
     */
    private func setupUI() {
        //添加子控件
        view.addSubview(collectionView)
        view.addSubview(toolbar)
        //布局子控件
        collectionView.translatesAutoresizingMaskIntoConstraints = false
        toolbar.translatesAutoresizingMaskIntoConstraints = false
        var cons = [NSLayoutConstraint]()
        let dict = ["collectionView": collectionView, "toolbar": toolbar]
        cons += NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[collectionView]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: dict)
        cons += NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[toolbar]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: dict)
        cons += NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[collectionView]-[toolbar(44)]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: dict)
        
        view.addConstraints(cons)
    }
    
    func itemClick(item: UIBarButtonItem){
        collectionView.scrollToItemAtIndexPath(NSIndexPath(forItem: 0, inSection: item.tag), atScrollPosition: UICollectionViewScrollPosition.Left, animated: true)
    }
    
    //MARK: - 懒加载
    private lazy var collectionView: UICollectionView = {
        let clv = UICollectionView(frame: CGRectZero, collectionViewLayout: EmoticonLayout())
        //注册cell
        clv.registerClass(EmoticonCell.self, forCellWithReuseIdentifier: EmoticonCellReuseIdentifier)
        clv.dataSource = self
        clv.delegate = self
        return clv
    }()
  
    private lazy var toolbar: UIToolbar = {
        let bar = UIToolbar()
        bar.tintColor = UIColor.darkGrayColor()
        var items = [UIBarButtonItem]()
        
        var index = 0
        for title in ["最近", "默认", "emoji", "浪小花"] {
            let item = UIBarButtonItem(title: title, style: UIBarButtonItemStyle.Plain, target: self, action: "itemClick:")
            item.tag = index++
            items.append(item)
            items.append(UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil))
        }
        items.removeLast()
        bar.items = items
        
        return bar
    }()
    
    private lazy var packages: [EmoticonPackage] = EmoticonPackage.packageList
}

extension EmoticonViewController: UICollectionViewDataSource, UICollectionViewDelegate {
    //告诉系统有多少组
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return packages.count
    }
    // 告诉系统每组有多少行
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return packages[section].emoticons?.count ?? 0
    }
    // 告诉系统每行显示什么内容
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(EmoticonCellReuseIdentifier, forIndexPath: indexPath) as! EmoticonCell
        
        cell.backgroundColor = (indexPath.item % 2 == 0) ? UIColor.blueColor() : UIColor.yellowColor()
        
        //1.取出对应的组
        let package = packages[indexPath.section]
        //2.取出对应组对应的模型
        let emoticon = package.emoticons![indexPath.item]
        //3.赋值给cell
        cell.emoticon = emoticon
        return cell
    }
    //选中某个cell时调用
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        
        //1.将当前使用的表情添加到最近表情数组中
        let emoticon = packages[indexPath.section].emoticons![indexPath.item]
        emoticon.times++
        packages[0].appendEmoticon(emoticon)
//        collectionView.reloadSections(NSIndexSet(index: 0))
        //2.回调通知使用着当前点击了那个表情
        emoticonDidSelectedCallback(emoticon: emoticon)
    }
}

class EmoticonCell: UICollectionViewCell {
    
    var emoticon: Emoticon? {
        didSet{
            //判断是否为图片表情
            if emoticon!.chs != nil {
                iconButton.setImage(UIImage(contentsOfFile: emoticon!.imagePath!), forState: .Normal)
            }else {
                //防止重用
                iconButton.setImage(nil, forState: .Normal)
            }
            
            //设置emoji表情
            //加上??防止重用
            iconButton.setTitle(emoticon!.emojiStr ?? "", forState: .Normal)
            
            //判断是否为删除按钮
            if emoticon!.isRemoveButton {
                iconButton.setImage(UIImage(named: "compose_emotion_delete"), forState: .Normal)
                iconButton.setImage(UIImage(named: "compose_emotion_delete_highlighted"), forState: .Highlighted)
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    /**
     初始化UI
     */
    func setupUI() {
        contentView.addSubview(iconButton)
//        iconButton.frame = contentView.bounds
        iconButton.backgroundColor = UIColor.whiteColor()
        iconButton.frame = CGRectInset(contentView.bounds, 4, 4)
        iconButton.userInteractionEnabled = false
    }
    
    //MARK: - 懒加载
    private lazy var iconButton: UIButton = {
        let btn = UIButton()
        btn.titleLabel?.font = UIFont.systemFontOfSize(32)
       return btn
    }()
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}


/// 自定义布局
class EmoticonLayout: UICollectionViewFlowLayout {
    override func prepareLayout() {
        super.prepareLayout()
        //设置cell相关属性
        let width = collectionView!.bounds.width / 7
        itemSize = CGSize(width: width, height: width)
        minimumInteritemSpacing = 0
        minimumLineSpacing = 0
        scrollDirection = UICollectionViewScrollDirection.Horizontal
        //设置collectionView的一些相关属性
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
        collectionView?.showsHorizontalScrollIndicator = false
        
        //注意:最好不要除以0.5，因为CGFloat不准确
        let y = (collectionView!.bounds.height - 3 * width) * 0.45
        collectionView?.contentInset = UIEdgeInsets(top: y, left: 0, bottom: y, right: 0)
    }
}